home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 187_01 / dt_parse.c < prev    next >
C/C++ Source or Header  |  1986-01-08  |  9KB  |  183 lines

  1. /*@*****************************************************/
  2. /*@                                                    */
  3. /*@ time_parse - parse a time parameter.               */
  4. /*@        Format is -t{HH{:MM{:SS}}},{HH{:MM{:SS}}}   */
  5. /*@        The first is the minimum value, the         */
  6. /*@        second is the maximum.  Missing minimum     */
  7. /*@        values default to 00, missing maximum       */
  8. /*@        values default to 99.                       */
  9. /*@                                                    */
  10. /*@   Usage:     time_parse(p);                        */
  11. /*@       where p is a pointer to the input arg.       */
  12. /*@       Returns p pointing to the end of string.     */
  13. /*@       Exits via call to error() on error.          */
  14. /*@                                                    */
  15. /*@*****************************************************/
  16.  
  17. #include    "dtparse.mon"
  18.  
  19. char strbuf[30];        /* string work area */
  20.  
  21. /***********************************************************************/
  22. /*                                                                     */
  23. /*    Parse time input parameters.                                       */
  24. /*                                                                     */
  25. /***********************************************************************/
  26.  
  27. char *time_parse(p)
  28. char *p;
  29. {
  30.     int i, strlen(), strcat(), len;
  31.     char ct, ct2, *dt_parse();
  32.  
  33.     p = dt_parse(p, time_sel.min_sel, ':');
  34.     if ((len = strlen(time_sel.min_sel)) > sizeof(time_sel.min_sel))
  35.             error("-t minimum parameter too long. Max is ", itoa(sizeof(time_sel.min_sel)-1, strbuf));
  36.     if (time_sel.min_sel[0]) {          /* if parameter was given */
  37.            ct = time_sel.min_sel[2];    /* first possible colon */
  38.            ct2 = time_sel.min_sel[5];    /* second possible colon */
  39.            if (ct)
  40.                if ((ct != ':') || (ct2 && (ct2 != ':')))     /* and no colon */
  41.              error("-t minimum not in HH{:MM{:SS}} format.", "");
  42.          strncat(time_sel.min_sel, ":00:00", sizeof(time_sel.min_sel)-len-1);    /* normalize */
  43.          time_sel.min_sel[sizeof(time_sel.min_sel)-1] = 0x00;    /* EOS */
  44.     }
  45.     if (*p == ',')
  46.      p++;                /* skip comma */
  47.     p = dt_parse(p, time_sel.max_sel, ':');
  48.     if ((len = strlen(time_sel.max_sel)) > sizeof(time_sel.max_sel))
  49.             error("-t maximum parameter too long. Max is ", itoa(sizeof(time_sel.max_sel)-1, strbuf));
  50.     if (time_sel.max_sel[0]) {          /* if parameter was given */
  51.            ct = time_sel.max_sel[2];    /* first possible colon */
  52.            ct2 = time_sel.max_sel[5];    /* second possible colon */
  53.            if (ct)
  54.                if ((ct != ':') || (ct2 && (ct2 != ':')))     /* and no colon */
  55.                    error("-t maximum not in HH{:MM{:SS}} format.", "");
  56.            strncat(time_sel.max_sel, ":99:99", sizeof(time_sel.max_sel)-len-1);    /* normalize */
  57.            time_sel.max_sel[sizeof(time_sel.max_sel)-1] = 0x00;    /* EOS */
  58.     }
  59.     return p;
  60. }
  61.  
  62. /*@*****************************************************/
  63. /*@                                                    */
  64. /*@ date_parse - parse a date parameter.               */
  65. /*@        Format is -d{MM{-DD{-YY}}},{MM{-DD{-YY}}}   */
  66. /*@        The first is the minimum value, the         */
  67. /*@        second is the maximum.  Missing minimum     */
  68. /*@        values default to 00, missing maximum       */
  69. /*@        values default to 99.                       */
  70. /*@                                                    */
  71. /*@   Usage:     date_parse(p);                        */
  72. /*@       where p is a pointer to the input arg.       */
  73. /*@       Returns p pointing to the end of string.     */
  74. /*@       Exits via call to error() on error.          */
  75. /*@                                                    */
  76. /*@*****************************************************/
  77.  
  78. /***********************************************************************/
  79. /*                                                                     */
  80. /*    Parse date input parameters.                                       */
  81. /*                                                                     */
  82. /***********************************************************************/
  83.  
  84. char *date_parse(p)
  85. char *p;
  86. {
  87.     int i, strlen(), isdigit(), yymmdd(), strcat(), len, dates();
  88.     char ct, ct2, *dt_parse();
  89.  
  90.     p = dt_parse(p, date_sel.min_sel, '-');
  91.     if ((len = strlen(date_sel.min_sel)) > sizeof(date_sel.min_sel))
  92.            error("-d parameter too long. Max is ", itoa(sizeof(date_sel.min_sel)-1, strbuf));
  93.     if (date_sel.min_sel[0]) {          /* if parameter was given */
  94.            ct = date_sel.min_sel[2];    /* first possible dash */
  95.            ct2 = date_sel.min_sel[5];    /* second possible dash */
  96.            if (ct)
  97.                if ((ct != '-') || (ct2 && (ct2 != '-')))     /* and no dash */
  98.                   error("-d minimum not in MM{-DD{-YY}} format.", "");
  99.           if (len == 2) {
  100.               strcat(date_sel.min_sel, "-00-");    /* normalize */
  101.               dates(strbuf);                        /* get year */
  102.               strcat(date_sel.min_sel, &strbuf[6]);
  103.           }
  104.           else if (len == 5) {
  105.               strcat(date_sel.min_sel, "-");        /* normalize */
  106.               dates(strbuf);                        /* get year */
  107.               strcat(date_sel.min_sel, &strbuf[6]);
  108.           }
  109.            date_sel.min_sel[sizeof(date_sel.min_sel)-1] = 0x00;    /* EOS */
  110.            if ((!isdigit(date_sel.min_sel[3])) || (!isdigit(date_sel.min_sel[6])))
  111.                error("-d minimum not in MM{-DD{-YY}} format.", "");
  112.            yymmdd(date_sel.min_sel, '-');        /* change it to yymmdd form */
  113.     }
  114.     if (*p == ',')
  115.        p++;                /* skip comma */
  116.     p = dt_parse(p, date_sel.max_sel, '-');
  117.     if ((len = strlen(date_sel.max_sel)) > sizeof(date_sel.max_sel))
  118.            error("-d parameter too long. Max is ", itoa(sizeof(date_sel.max_sel)-1, strbuf));
  119.     if (date_sel.max_sel[0]) {          /* if parameter was given */
  120.          ct = date_sel.max_sel[2];    /* first possible dash */
  121.          ct2 = date_sel.max_sel[5];    /* second possible dash */
  122.          if (ct)
  123.              if ((ct != '-') || (ct2 && (ct2 != '-')))     /* and no dash */
  124.                    error("-d maximum not in MM{-DD{-YY}} format.", "");
  125.           strncat(date_sel.max_sel, "-99-99", sizeof(date_sel.max_sel)-len-1);    /* normalize */
  126.            date_sel.max_sel[sizeof(date_sel.max_sel)-1] = 0x00;    /* EOS */
  127.            if ((!isdigit(date_sel.max_sel[3])) || (!isdigit(date_sel.max_sel[6])))
  128.                error("-d maximum not in MM{-DD{-YY}} format.", "");
  129.           yymmdd(date_sel.max_sel, '-');        /* change it to yymmdd form */
  130.     }
  131.     return p;
  132. }
  133.  
  134. /*@*****************************************************/
  135. /*@                                                    */
  136. /*@ dt_parse - get one date/time value.                */
  137. /*@        Expands one digit values by adding a        */
  138. /*@        leading zero.  Allows a variable number     */
  139. /*@        of delimited groups of one or two digits.   */
  140. /*@                                                    */
  141. /*@   Usage:     dt_parse(p, s, c);                    */
  142. /*@       where p is a pointer to the input arg.       */
  143. /*@             s is a pointer to the output area.     */
  144. /*@             c is a char holding the delimiter.     */
  145. /*@                                                    */
  146. /*@     Returns the updated pointer p.                 */
  147. /*@                                                    */
  148. /*@*****************************************************/
  149.  
  150. /***********************************************************************/
  151. /*                                                                     */
  152. /*     Parse a date/time parameter.                                      */
  153. /*                                                                     */
  154. /*   Assumes that input is xxcxxcxx where the xx are integers and      */
  155. /*      c is the given separator.  If only one digit is given for      */
  156. /*      any pair, a leading zero is added to normalize it.             */
  157. /*                                                                     */
  158. /***********************************************************************/
  159.  
  160. char *dt_parse(p, s, c)
  161. char *p, *s, c;
  162. {
  163.     char *save;
  164.  
  165.     while (*p && (*p != ',')) {
  166.         save = s;            /* save start of output area */
  167.         while (*p